﻿using System;
using imaginecup.util;
using imaginecup.objectManager;
using imaginecup.information;
using Microsoft.Xna.Framework;
using Imaginecup.gameState.malaria.componentInit;
using Imaginecup.gameState.malaria.manager;
using Imaginecup.sprite.animation;
using Imaginecup.gameState.malaria.component.etc;
using Imaginecup.gameState.malaria.component.entity;
using Imaginecup.sprite;
using System.Collections.Generic;
using Imaginecup.util.collision;
using Imaginecup.util;
using Imaginecup.sprite.factory;

namespace Imaginecup.gameState.malaria.component.children
{    
    public class CmpChildren: BaseEntityComponent ,  ICmpChar
    {       
        private float m_Demage;
        private IControlAnimationIndex m_IndexControl;
        private GObjectID m_ProgressBarID;
        private bool m_bShieldState;
        private double m_ShieldTime;

        private double m_Velocity;

        public CmpChildren(GObjectID a_GID, ObjectManager objMan )// , IControlAnimationIndex control)
            : base(a_GID, objMan, ComponentTypeID.CID_CHILDREN_ENTITY)
        {
            m_Demage = 0;

            m_bShieldState = false;
            m_ShieldTime = 0;

            m_ProgressBarID = new GObjectID();
            m_ObjectMan.AddComponent(new CmpProgressBar(m_ProgressBarID, m_ObjectMan, this));
        }

        public void SetVelocity(double velocity)
        {
            m_Velocity = velocity;
        }

        public override void Init()
        {
            AnimationIndexMan indexMan = new AnimationIndexMan();
            indexMan.SetDelay(0.4);
            indexMan.SetIndex(new Vector2(0 , 2), 4);


            m_IndexControl = indexMan;
            m_IndexControl.StartAnimation();
                        
            FactorySpriteInfo spriteInfo = new FactorySpriteInfo( SPRITE_TYPE_ID.ANIMATION_SPRITE , "Children" , indexMan);
            SpriteFactory spriteFactory = new SpriteFactory();


            ICmpRender render = new CmpDefaultRender(m_ObjectID, m_ObjectMan, (ISprite)spriteFactory.GetObject(spriteInfo),   ComponentTypeID.CID_RENDER_CHILD);

            m_ObjectMan.AddComponent(render);

            base.Init();
        }

        public override void Release()
        {
        }

        public override void Update(GameTime gameTime)
        {
            m_IndexControl.Update(gameTime);
            UpdateChildren(gameTime);

            if (m_bShieldState)
            {
                m_ShieldTime -= gameTime.ElapsedGameTime.TotalSeconds;
                if (m_ShieldTime <= 0)
                {
                    m_ShieldTime = 0;
                    m_bShieldState = false;
                }
            }
        }

        public override EMessageResult HandleMessage(GMessage a_Msg)
        {
           if (a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_CHECK_STATE.MessageTypeID)
            {
                IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();
                if( Collision.CollisionByPoint(m_Info , mouseInput.Position))
                {
                    //눌려졌으면 눌려진 객체를 등록.
                    List<ICmpChar> list = (List<ICmpChar>)a_Msg.m_Data;

                    list.Add(this);
                }
                
            }
            return base.HandleMessage(a_Msg);
        }

        private void UpdateChildren(GameTime gameTime)
        {
            //데미지를 100이상 받으면 죽는다.
            if( m_Demage > 100 )
            {
                m_ObjectMan.DestroyObject(m_ObjectID);
                LifeMan.GetLifeMan().DecreaseLife();

                GMessage msg = new GMessage(MessageTypeID.MT_CHILD_DIE , m_ObjectID);
                m_ObjectMan.BroadCastMessage(msg);

                m_ObjectMan.DestroyObject(m_ProgressBarID);
                return;
            }

            m_Info.mPosition.X += (float)( m_Velocity * gameTime.ElapsedGameTime.TotalSeconds );

            if( m_Info.mPosition.X < 0 )
            {
                GMessage msg = new GMessage(MessageTypeID.MT_CHILD_DIE, m_ObjectID);
                m_ObjectMan.BroadCastMessage(msg);
                m_ObjectMan.DestroyObject(m_ObjectID);

                m_ObjectMan.DestroyObject(m_ProgressBarID);
                return;
            }

            
            GMessage Msg = new GMessage();
            Msg.m_Data = this;
            Msg.m_FromID = m_ObjectID;
            Msg.m_Type = MessageTypeID.MT_CHANGE_CHILDRENPOSITION;

            m_ObjectMan.BroadCastMessage(Msg);
        }

        public bool GetShieldState()
        {
            return m_bShieldState;
        }

        public void SetShieldTime(double time)
        {
            m_ShieldTime = time;
        }

        public void StartShieldItem()
        {
            m_bShieldState = true;
        }

        public int GetDemage()
        {
            if (m_Demage > 100) return 100;
            return (int)m_Demage;
        }

        public void AddDemage(float Demage)
        {
            if (m_bShieldState) return;

            m_Demage += Demage;

            if (m_Demage < 0)
                m_Demage = 0;
        }
    }
}
